package com.jinmei.web.action;


import org.apache.commons.lang.StringUtils;
import org.apache.struts2.json.annotations.JSON;

import com.jinmei.jerplogin.JERPRequestConsts;
import com.jinmei.jerplogin.JERPLoginResponse;
import com.jinmei.jerplogin.JERPLogoutResponse;
import com.jinmei.remote.client.JERPRequest;
import com.jinmei.utils.JinmeiConsts;
import com.opensymphony.xwork2.ActionContext;

public class AppLoginAction extends BaseAction {

	private static final long serialVersionUID = -6695021473119446046L;

	private String userid;
	private String password;
	
	// json result
	private String status;
	private String msg;

	public String login() throws Exception {
		JERPLoginResponse jerpRepsonse = callJERPLoginService();
		String loginStatus = jerpRepsonse.getLoginStatus();
		
		if (StringUtils.equals(loginStatus, JinmeiConsts.JERP_LOGIN_SUCCESS_CODE)) {
			// for app connection
			ActionContext.getContext().getSession().put(
					JinmeiConsts.USER_SESSIONID, request.getSession().getId());
			// for jerp logout
			ActionContext.getContext().getSession().put(
					JinmeiConsts.JERP_SESSIONID, jerpRepsonse.getSessionId());
			
			setUser_sessionid(request.getSession().getId());
			setStatus(SUCCESS);
		} else {
			setMsg(jerpRepsonse.getLoginMsg());
			setStatus(ERROR);
		}
		
		return SUCCESS;
      
	} 
	
	public String logout() throws Exception {
		JERPLogoutResponse repsonse = callJERPLogoutService();
		String logoutStatus = repsonse.getLogoutStatus();
		
		if (StringUtils.equals(logoutStatus, JinmeiConsts.JERP_LOGIN_SUCCESS_CODE)) {
			// store session id info
			ActionContext.getContext().getSession().remove(JinmeiConsts.USER_SESSIONID);
			ActionContext.getContext().getSession().remove(JinmeiConsts.JERP_SESSIONID);
			
			// pass status to app as json
			setStatus(SUCCESS);
		} else {
			setStatus(ERROR);
		}
		
		return SUCCESS;
      
	}  
	
	private JERPLoginResponse callJERPLoginService() throws Exception {
		JERPRequest jerpRequest = new JERPRequest();
		jerpRequest.addNameValuePair(JERPRequestConsts.SACCTNUM, JERPRequestConsts.SACCTNUM_VALUE);
		jerpRequest.addNameValuePair(JERPRequestConsts.SLOGINNAME, getUserid());
		jerpRequest.addNameValuePair(JERPRequestConsts.SPWD, getPassword());
		jerpRequest.sendRequest(JERPRequestConsts.JREP_LOGIN_URL);
		
		return (JERPLoginResponse) jerpRequest.getResponse(JERPLoginResponse.class);
	}
	
	private JERPLogoutResponse callJERPLogoutService() throws Exception {
		JERPRequest jerpRequest = new JERPRequest();
		jerpRequest.addNameValuePair(JERPRequestConsts.SCLIENTSESSION, request.getSession().getId());
		jerpRequest.sendRequest(JERPRequestConsts.JREP_LOGOUT_URL);
		
		return (JERPLogoutResponse) jerpRequest.getResponse(JERPLogoutResponse.class);
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	@JSON (name="status")
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	
	/* 
	 * need override and annotate here,
	 * we cannot recognize it in parent class 
	 */
	@JSON (name="user_sessionid")
	public String getUser_sessionid() {
		return user_sessionid;
	}

	@JSON (name="msg")
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
}
